/*
 * 练习8：
 */
package chapter6_AccessControl;

class Balls{
	private static int count = 0;
	private int i = 0;
	private Balls() { System.out.println("Balls()");}
	// Allow creation via static method:
	static Balls makeBalls() {
		count++;
		return new Balls();
	}
	public static int howMany() { return count; } 
	public String toString() {
		return ("Ball " + count);
	}
}
public class E8_ObjectManagement {
	
	static int howManyLeft = 3;
	static Balls[] ca = new Balls[3];	
	{
	for(Balls x : ca)
		x = Balls.makeBalls();
	}
	public static Balls getBalls() {
		if(howManyLeft > 0)
			return ca[--howManyLeft];
		else {
			System.out.println("No more Ballss");
			return null;
		}
	}
	public static void main(String[] args) {		
		E8_ObjectManagement cm = new E8_ObjectManagement();
		System.out.println(cm.howManyLeft);
		cm.getBalls();
		System.out.println(howManyLeft);
		cm.getBalls();
		System.out.println(howManyLeft);
		cm.getBalls();
		System.out.println(cm.getBalls());
		System.out.println(howManyLeft);
	}				  	

}
